﻿using Microsoft.Practices.Unity;
using Model;
using Service;
using Strawberry.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Drawing;

namespace Strawberry.Controllers
{
    public partial class AdminController : Controller
    {
        [Dependency]
        public IUserManager UserManager { get; set; }

		[Dependency]
        public IArticleManager ArticleManager { get; set; }
		
        [Dependency]
        public IJobOfferManager JobOfferManager { get; set; }

	    [Dependency]
        public IContactUsManager ContactUsManager { get; set; }

        [Dependency]
        public IImageManager ImageManager { get; set; }

        [Dependency]
        public IMailService MailService { get; set; }

        [Dependency]
        public IArticleImageManager ImageArticleManager { get; set; }

        public virtual ActionResult Users()
        {
            var user = UserManager.Query().OrderBy(x => x.Name);
            
            return View(user);
        }        

        public virtual ActionResult UserAdd()
        {
            return View();
        }

        /// <summary>
        /// Add an user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult UserAdd(User user)
        {
            //var file = Request.Files["picture"];

            string path = Path.GetFileName(Request.FilePath);//AppDomain.CurrentDomain.BaseDirectory;
            
            foreach (string upload in Request.Files)
            {
                Console.WriteLine("rien");
                //if (!Request.Files[upload].HasFile()) continue;
                /*path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
                string filename = Path.GetFileName(Request.Files[upload].FileName);
                Request.Files[upload].SaveAs(Path.Combine(path, filename));*/
            }
            
            using (MemoryStream memoryStream = new MemoryStream())
            user.Role = Role.User;
            Model.Image image = ImageManager.getLastImageSaved();
            user.Image = image;
            UserManager.Add(user);

            return RedirectToAction(MVC.Admin.Users());
        }

        [HttpPost]
        public virtual ActionResult UserUpdate(User user)
        {
            user.Role = Role.User;
            UserManager.Update(user);
            return RedirectToAction(MVC.Admin.Users());
        }

        public virtual ActionResult UserUpdate(int Id)
        {
            User user = UserManager.Get(Id);
            return View(user);
        }

        public virtual ActionResult UserDelete(int Id)
        {
            User user = UserManager.Get(Id);
            UserManager.Delete(user);
            return RedirectToAction(MVC.Admin.Users());
        }

        #region ARTICLES

        // LISTES D'ARTICLES
        public virtual ActionResult Articles()
        {
            var list = ArticleManager.Query();

            return View(list);
        }

        // AJOUT D'ARTICLE
        public virtual ActionResult ArticleAdd()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult ArticleAdd(Article Article)
        {
            ArticleManager.Add(Article);          
            
            return RedirectToAction(MVC.Admin.Articles());
        }

        // Article update
        public virtual ActionResult ArticleUpdate(int Id)
        {
            Article article = ArticleManager.Get(Id);
            return View(article);
        }

        [HttpPost]
        public virtual ActionResult ArticleUpdate(Article article)
        {
            ArticleManager.Update(article);
            return RedirectToAction(MVC.Admin.Articles());
        }

        //Article delete
        public virtual ActionResult ArticleDelete(int Id)
        {
            Article article = ArticleManager.Get(Id);
            ArticleManager.Delete(article);
            return RedirectToAction(MVC.Admin.Articles());
        }
#endregion

        #region JOBOFFERS

        public virtual ActionResult JobOffers()
        {
            var jobs = JobOfferManager.Query();
            return View(jobs);
        }

        public virtual ActionResult JobOffersAdd()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult JobOffersAdd(JobOffer JobOffer)
        {
            JobOfferManager.Add(JobOffer);
            return RedirectToAction(MVC.Admin.JobOffers());
        }

        public virtual ActionResult JobOffersUpdate(int id)
        {
            var offer = JobOfferManager.Get(id);
            return View(offer);
        }

        [HttpPost]
        public virtual ActionResult JobOffersUpdate(JobOffer JobOffer)
        {
            JobOfferManager.Update(JobOffer);
            return RedirectToAction(MVC.Admin.JobOffers());
        }

        public virtual ActionResult JobOffersDelete(int id)
        {
            var offer = JobOfferManager.Get(id);
            JobOfferManager.Delete(offer);
            return RedirectToAction(MVC.Admin.JobOffers());
        }

        #endregion

        #region MAIL
        /// <summary>
        /// Display user messages list
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public virtual ActionResult UserMessages()
        {
            var messages = ContactUsManager.Query();
            return View();
        }

        /// <summary>
        /// To answer an user question
        /// </summary>
        /// <returns></returns>
        public virtual ActionResult UserAnswer(int id)
        {
            var contact = ContactUsManager.Get(id);
            var model = new Answer()
            {
                Mail = contact.Email
            };
            return View(model);
        }
        /// <summary>
        /// Get values et send a mail
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public virtual ActionResult UserAnswer(Answer answer)
        {
            var title = answer.Title;
            var content = answer.Content;
            var mail = answer.Mail;
            MailService.SendMail(null, title, content,mail);
            return RedirectToAction(MVC.Admin.UserMessages());
        }

        /// <summary>
        /// Delete a user message
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public virtual ActionResult UserMessageDelete(int Id)
        {
            ContactUs contactUs = ContactUsManager.Get(Id);
            ContactUsManager.Delete(contactUs);
            return RedirectToAction(MVC.Admin.UserMessages());
        }

        #endregion

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public virtual JsonResult SaveDynamicPicture()
        {
            if (Request.InputStream != null)
            {
                // IE A essayé if (String.IsNullOrEmpty(Request["qqfile"])) {
                string name = Request["qqfile"];

                string url = @"C:\temp\" + name;
                using (Bitmap b = new Bitmap(Request.InputStream))
                {
                    b.Save(url);
                }
                
                Model.Image image = new Model.Image {
                    Description = name,
                    Url = url,
                };
                ImageManager.Add(image);
                
                
            }
            return Json(new { success = true }, "application/json");
        }
       

    }
}
